package com.lantosec.hmrp.core.exception;

import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.ResultEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局错误处理
 *
 * @author 杨成宇
 * @version 1.0
 */
@ControllerAdvice(basePackages = "com.lantosec.hmrp")
@Log4j2
public class GlobalControllerExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = AppException.class)
    public Result appExceptionHandler(AppException ex){
        //TODO 发生异常进行日志记录，写入数据库或者其他处理
        log.error(ex.getMessage(), ex);
        return Result.fail(ex.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(Exception ex) {
        //TODO 发生异常进行日志记录，写入数据库或者其他处理
        log.error(ex.getMessage(), ex);
        return Result.by(ResultEnum.EXCEPTION, ex);
    }

    @ResponseBody
    @ExceptionHandler(value = AuthorizationException.class)
    public Result ErrorHandler(AuthorizationException ex) {
        log.error(ex.getMessage(), ex);
        return Result.fail("无权访问");
    }
}
